import 'dart:ui';

import 'package:flutter/material.dart';

class DkColor extends Color {
  DkColor(final String hexColor) : super(color2Hex(hexColor));
}

int color2Hex(String hexColor) {
  hexColor = hexColor.toUpperCase().replaceAll("#", "");
  hexColor = hexColor.replaceAll('0X', '');
  if (hexColor.length == 6) {
    hexColor = "FF" + hexColor;
  }
  int result;
  try {
    result = int.parse(hexColor, radix: 16);
  } catch (e) {
    result = 0x00000000;
  }
  return result;
}

int hex2Int(String hex) {
  int val = 0;
  int len = hex.length;
  for (int i = 0; i < len; i++) {
    int hexDigit = hex.codeUnitAt(i);
    if (hexDigit >= 48 && hexDigit <= 57) {
      val += (hexDigit - 48) * (1 << (4 * (len - 1 - i)));
    } else if (hexDigit >= 65 && hexDigit <= 70) {
      // A..F
      val += (hexDigit - 55) * (1 << (4 * (len - 1 - i)));
    } else if (hexDigit >= 97 && hexDigit <= 102) {
      // a..f
      val += (hexDigit - 87) * (1 << (4 * (len - 1 - i)));
    } else {
      throw new FormatException("Invalid hexadecimal value");
    }
  }
  return val;
}
